;
(function(root) {
    if (typeof exports !== 'undefined') {
        if (typeof module !== 'undefined' && module.exports) {
            exports = module.exports = Circlemodule;
        }
        exports.Circlemodule = Circlemodule;
    } else {
        root.Circlemodule = Circlemodule;
    }

    function Circlemodule(initData, options, callback) {
        this.initData = initData
        this.options = options
        this.callback = callback
    }

    function Circle(x, y, r, bgColor, text, textColor) {

        this.x = x;
        this.y = y;
        this.r = r;
        this.bgColor = bgColor;
        var ele = document.createElement('div');
        ele.style.position = 'absolute';
        ele.style.left = x + 'px';
        ele.style.top = y + 'px';
        ele.style.width = r + 'px';
        ele.style.height = r + 'px';
        ele.style.marginLeft = -r / 2 + 'px';
        ele.style.marginTop = -r / 2 + 'px';
        ele.style.borderRadius = '50%';
        ele.style.backgroundColor = bgColor;
        bgColorHightActive: "#f56f00",
            ele.style.color = textColor;
        ele.textContent = text;
        ele.zIndex = 9999999;
        ele.classList.add('circle');
        return ele
    }

    function getComputePosition(n, r) {
        if (n < 0 || r < 0) {
            return Error('n or r must be positive int')
        }
        var arr = [];
        var theta = 2 * Math.PI / n
        if (n == 1) {
            return [{
                x: r,
                y: 0
            }]
        } else {
            if (n % 2 == 0) {
                for (var i = 0; i < n; i++) {
                    if (i == 0) {
                        arr.push({
                            x: r,
                            y: 0
                        })
                    } else if (i == n / 2) {
                        arr.push({
                            x: r,
                            y: 2 * r
                        })
                    } else if (i == n / 4) {
                        arr.push({
                            x: 2 * r,
                            y: r
                        })
                    } else if (i == n / 4 + n / 2) {
                        arr.push({
                            x: 0,
                            y: r
                        })
                    } else {
                        var obj = {
                            x: (i < n / 2) ? (r + Math.abs(Math.sin(theta * i) * r)) : (r - Math.abs(Math.sin(theta * i) * r)),
                            y: (i < 3 * n / 4 && i > n / 4) ? r + Math.abs(Math.cos(theta * i) * r) : r - Math.abs(Math.cos(theta * i) * r)
                        }
                        arr.push(obj)
                    }
                }
            }
            // n Ϊ����
            if (n % 2 == 1) {
                for (var i = 0; i < n; i++) {
                    if (i == 0) {
                        arr.push({
                            x: r,
                            y: 0
                        })
                    } else {
                        var obj = {
                            x: (i < n / 2) ? (r + Math.abs(Math.sin(theta * i) * r)) : (r - Math.abs(Math.sin(theta * i) * r)),
                            y: (i < 3 * n / 4 && i > n / 4) ? r + Math.abs(Math.cos(theta * i) * r) : r - Math.abs(Math.cos(theta * i) * r)
                        }
                        arr.push(obj)
                    }
                }
            }

        }
        return arr;
    }

    Circlemodule.prototype.init = function(selector) {
        var initData = this.initData;
        var options = this.options;
        var callback = this.callback;
        var $container = $(selector);
        var $outerCircle = $container.find(".circle-outer");
        var $innerCircle = $container.find(".circle-inner");

        // initbox
        $outerCircle.css({
            width: options.outerBoxRadius + 'px',
            height: options.outerBoxRadius + 'px',
            marginLeft: -options.outerBoxRadius / 2 + 'px',
            marginTop: -options.outerBoxRadius / 2 + 'px'
        })
        $innerCircle.css({
            width: options.innerBoxRadius + 'px',
            height: options.innerBoxRadius + 'px',
            marginLeft: -options.innerBoxRadius / 2 + 'px',
            marginTop: -options.innerBoxRadius / 2 + 'px'
        })

        // init circle tags
        var circle = [];
        var outerCoordinates = getComputePosition(initData.outerData.length, $outerCircle.width() / 2)
        outerCoordinates.forEach(function(coordinate, index) {
            var ele = new Circle(
                coordinate.x,
                coordinate.y,
                initData.outerData[index].radius,
                initData.outerData[index].bgColor,
                initData.outerData[index].text,
                initData.outerData[index].textColor,
                initData.innerData.hightLight
            );
            $outerCircle.append(ele);
        })

        var innerCoordinates = getComputePosition(initData.innerData.length, $innerCircle.width() / 2)
        innerCoordinates.forEach(function(coordinate, index) {
            var ele = new Circle(
                coordinate.x,
                coordinate.y,
                initData.innerData[index].radius,
                initData.innerData[index].bgColor,
                initData.innerData[index].text,
                initData.innerData[index].textColor,
                initData.innerData.hightLight

            );
            $innerCircle.append(ele);
        })

        // bindEvent
        $(selector).find(".circle").on("click", function(e) {
            if (typeof callback.onClick == 'function') {
                callback.onClick(e);
            }
        })

    }

    Circlemodule.prototype.update = function(selector) {
        $(selector).find('.circle-outer').html('');
        $(selector).find('.circle-inner').html('');
        this.init(selector)
    }


})(window || global);